package com.vrv.netmgr.entry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.adapter.VRVExpandableListAdapter;
import com.vrv.common.util.DataSetUtil;
import com.vrv.common.util.StringUtils;
import com.vrv.netmgr.R;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 策略中心导航主界面
 *
 * @author gaopei
 * @date
 */
public class StrategyActivity extends Activity {
	private boolean isBack;

	private Boolean isNavgate = Boolean.TRUE;

	private Boolean isTop = true;

	private LinearLayout leverTwoLayout;

	private ExpandableListAdapter mAdapter;

	private ExpandableListView expandListView;

	private List<Map<String, String>> groups = new ArrayList<Map<String, String>>();

	private List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initViewSetting();
	}

	/**
	 * 加载初始化布局，初始化组件
	 */
	public void initViewSetting() {

		isTop = Boolean.TRUE;

		setContentView(R.layout.strategy_nine_cell);
		leverTwoLayout = (LinearLayout) this.findViewById(R.id.lever_2_layout);
		leverTwoLayout.setVisibility(View.VISIBLE);

		MainTab mainTab = (MainTab) getParent();
		mainTab.setTitle(getResources().getString(R.string.strategy), false);

		RelativeLayout nav1Layout = (RelativeLayout) this.findViewById(R.id.navSafeMgr);
		nav1Layout.setTag(Boolean.FALSE);
		nav1Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble0 = (TextView) findViewById(R.id.commentWidget_0);
		bubble0.setVisibility(View.VISIBLE);

		RelativeLayout nav2Layout = (RelativeLayout) this.findViewById(R.id.navBehaviourMgr);
		nav2Layout.setTag(Boolean.FALSE);
		nav2Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble1 = (TextView) findViewById(R.id.commentWidget_1);
		bubble1.setVisibility(View.VISIBLE);

		RelativeLayout nav3Layout = (RelativeLayout) this.findViewById(R.id.navBasicSafeMgr);
		nav3Layout.setTag(Boolean.TRUE);
		// nav3Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble2 = (TextView) findViewById(R.id.commentWidget_2);
		bubble2.setVisibility(View.GONE);

		RelativeLayout nav4Layout = (RelativeLayout) this.findViewById(R.id.navDataSafeMgr);
		nav4Layout.setTag(Boolean.FALSE);
		nav4Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble3 = (TextView) findViewById(R.id.commentWidget_3);
		bubble3.setVisibility(View.VISIBLE);

		RelativeLayout nav5Layout = (RelativeLayout) this.findViewById(R.id.navPatchDistrMgr);
		nav5Layout.setTag(Boolean.FALSE);
		nav5Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble4 = (TextView) findViewById(R.id.commentWidget_4);
		bubble4.setVisibility(View.VISIBLE);

		RelativeLayout nav6Layout = (RelativeLayout) this.findViewById(R.id.navPubStrategy);
		nav6Layout.setTag(Boolean.FALSE);
		nav6Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble5 = (TextView) findViewById(R.id.commentWidget_5);
		bubble5.setVisibility(View.VISIBLE);

		RelativeLayout nav7Layout = (RelativeLayout) this.findViewById(R.id.navStrategyQuery);
		nav7Layout.setTag(Boolean.FALSE);
		nav7Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble6 = (TextView) findViewById(R.id.commentWidget_6);
		bubble6.setVisibility(View.VISIBLE);

		nav1Layout.setOnClickListener(new ClickLLListener());
		nav2Layout.setOnClickListener(new ClickLLListener());
		nav3Layout.setOnClickListener(new ClickLLListener());
		nav4Layout.setOnClickListener(new ClickLLListener());
		nav5Layout.setOnClickListener(new ClickLLListener());
		nav6Layout.setOnClickListener(new ClickLLListener());
		nav7Layout.setOnClickListener(new ClickLLListener());

	}

	/**
	 * 切换到第三级布局
	 */
	public void changeToLever3ViewSetting() {
		isTop = Boolean.FALSE;

		setContentView(R.layout.lever_config_nine_cell);// 切换布局
		expandListView = (ExpandableListView) findViewById(R.id.expand_list_view);
		mAdapter = new VRVExpandableListAdapter(StrategyActivity.this, groups, R.layout.groups, childs, R.layout.childs);
		expandListView.setAdapter(mAdapter);
		expandListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				if (mAdapter.getChildrenCount(groupPosition) > 0) {
					Log.d("#caobl#", "" + groupPosition);
					return false;
				} else {

					Map<String, String> groupItem = groups.get(groupPosition);
					String activityName = groupItem.get("activityName");

					if (StringUtils.isBlank(activityName))
						return true;

					ComponentName componentName = new ComponentName(StrategyActivity.this, activityName);
					Intent intent = new Intent();
					intent.setComponent(componentName);
					isNavgate = Boolean.FALSE;
					overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
					startActivity(intent);

					return false;
				}
			}
		});
		expandListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

				Map<String, String> childItem = childs.get(groupPosition).get(childPosition);
				String activityName = childItem.get("activityName");

				if (StringUtils.isBlank(activityName))
					return true;

				ComponentName componentName = new ComponentName(StrategyActivity.this, activityName);
				Intent intent = new Intent();
				intent.setComponent(componentName);
				isNavgate = Boolean.FALSE;
				overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
				startActivity(intent);
				return false;
			}
		});
	}

	private class ClickLLListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// 1.初始化数据
			switch (v.getId()) {
			case R.id.navSafeMgr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(0, getResources().getString(R.string.strCfgMgr));
				break;
			case R.id.navBehaviourMgr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(1, getResources().getString(R.string.strBehaviourMgr));
				break;
			case R.id.navBasicSafeMgr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(2, getResources().getString(R.string.strBasicSafeMgr));
				break;
			case R.id.navDataSafeMgr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(3, getResources().getString(R.string.strDataSafeMgr));
				break;
			case R.id.navPatchDistrMgr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(4, getResources().getString(R.string.strPatchDistrMgr));
				break;
			case R.id.navPubStrategy:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(5, getResources().getString(R.string.strPubStrategy));
				break;
			case R.id.navStrategyQuery:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(6, getResources().getString(R.string.strStrategyQuery));
				break;
			}

			// 2.切换布局填充adapter
			changeToLever3ViewSetting();
		}
	}

	/**
	 * 初始化静态数据
	 *
	 * @param string
	 *
	 * @param i
	 */
	private void fillLever3DataSet(int index, String title) {

		MainTab mainTab = (MainTab) getParent();
		mainTab.setTitle(title, true);
		mainTab.getBackBtn().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		groups.clear();
		if (index == 0) {
			// [start] 安全准入管理

			Map<String, String> groupMap0 = new HashMap<String, String>();
			groupMap0.put("key", "0");
			groupMap0.put("group", "阻断违规接入管理");
			groupMap0.put("activityName", "");
			groups.add(groupMap0);

			Map<String, String> groupMap1 = new HashMap<String, String>();
			groupMap1.put("key", "0");
			groupMap1.put("group", "补丁与杀毒软件认证");
			groupMap1.put("activityName", "");
			groups.add(groupMap1);

			Map<String, String> groupMap2 = new HashMap<String, String>();
			groupMap2.put("key", "0");
			groupMap2.put("group", "进程服务注册表认证");
			groupMap2.put("activityName", "");
			groups.add(groupMap2);

			Map<String, String> groupMap3 = new HashMap<String, String>();
			groupMap3.put("key", "0");
			groupMap3.put("group", "终端接入认证");
			groupMap3.put("activityName", "");
			groups.add(groupMap3);

			Map<String, String> groupMap4 = new HashMap<String, String>();
			groupMap4.put("key", "0");
			groupMap4.put("group", "网关接入认证");
			groupMap4.put("activityName", "");
			groups.add(groupMap4);

			Map<String, String> groupMap5 = new HashMap<String, String>();
			groupMap5.put("key", "0");
			groupMap5.put("group", "VIFR接入认证");
			groupMap5.put("activityName", "");
			groups.add(groupMap5);

			Map<String, String> groupMap6 = new HashMap<String, String>();
			groupMap6.put("key", "0");
			groupMap6.put("group", "网关接入认证配置");
			groupMap6.put("activityName", "");
			groups.add(groupMap6);

			// [end] 安全准入管理
		} else if (index == 1) {
			// [start] 行为管理及审计

			Map<String, String> groupMap7 = new HashMap<String, String>();
			groupMap7.put("key", "1");
			groupMap7.put("group", "终端代理管理");
			groupMap7.put("activityName", "");
			groups.add(groupMap7);

			Map<String, String> groupMap8 = new HashMap<String, String>();
			groupMap8.put("key", "1");
			groupMap8.put("group", "主机连接监控策略");
			groupMap8.put("activityName", "");
			groups.add(groupMap8);

			Map<String, String> groupMap9 = new HashMap<String, String>();
			groupMap9.put("key", "1");
			groupMap9.put("group", "屏幕抓取策略");
			groupMap9.put("activityName", "");
			groups.add(groupMap9);

			Map<String, String> groupMap10 = new HashMap<String, String>();
			groupMap10.put("key", "1");
			groupMap10.put("group", "屏幕录像策略");
			groupMap10.put("activityName", "");
			groups.add(groupMap10);

			Map<String, String> groupMap11 = new HashMap<String, String>();
			groupMap11.put("key", "1");
			groupMap11.put("group", "...");
			groupMap11.put("activityName", "");
			groups.add(groupMap11);

			// [end] 行为管理及审计
		} else if (index == 2) {
			// [start] 基本安全管理

			Map<String, String> groupMap12 = new HashMap<String, String>();
			groupMap12.put("key", "2");
			groupMap12.put("group", "硬件设备控制");
			groupMap12.put("activityName", "");
			groups.add(groupMap12);

			Map<String, String> groupMap13 = new HashMap<String, String>();
			groupMap13.put("key", "2");
			groupMap13.put("group", "进程及软件管理");
			groupMap13.put("activityName", "");
			groups.add(groupMap13);

			Map<String, String> groupMap14 = new HashMap<String, String>();
			groupMap14.put("key", "2");
			groupMap14.put("group", "主机安全策略");
			groupMap14.put("activityName", "");
			groups.add(groupMap14);

			Map<String, String> groupMap15 = new HashMap<String, String>();
			groupMap15.put("key", "2");
			groupMap15.put("group", "违规外联监控");
			groupMap15.put("activityName", "");
			groups.add(groupMap15);
			// [end] 基本安全管理
		} else if (index == 3) {
			Map<String, String> groupMap16 = new HashMap<String, String>();
			groupMap16.put("key", index + "");
			groupMap16.put("group", "文档安全策略");
			groupMap16.put("activityName", "");
			groups.add(groupMap16);

			Map<String, String> groupMap17 = new HashMap<String, String>();
			groupMap17.put("key", index + "");
			groupMap17.put("group", "安全刻录审计");
			groupMap17.put("activityName", "");
			groups.add(groupMap17);

			Map<String, String> groupMap18 = new HashMap<String, String>();
			groupMap18.put("key", index + "");
			groupMap18.put("group", "存储管理");
			groupMap18.put("activityName", "");
			groups.add(groupMap18);

		}

		childs.clear();
		for (int i = 0; i < groups.size(); i++) {
			List<Map<String, String>> varList = new ArrayList<Map<String, String>>(0);
			if ((index == 2) && (i == 1)) {
				Map<String, String> childMap0 = new HashMap<String, String>();
				childMap0.put("child", "软件安装监控");
				childMap0.put("key", index + "");
				childMap0.put("activityName", "com.vrv.netmgr.strategy.SWSetupMonitorActivity");
				varList.add(childMap0);

				Map<String, String> childMap1 = new HashMap<String, String>();
				childMap1.put("child", "软件安装行为限制");
				childMap1.put("key", index + "");
				childMap1.put("activityName", "");
				varList.add(childMap1);

				Map<String, String> childMap2 = new HashMap<String, String>();
				childMap2.put("child", "进程执行监控");
				childMap2.put("key", index + "");
				childMap2.put("activityName", "");
				varList.add(childMap2);

				Map<String, String> childMap3 = new HashMap<String, String>();
				childMap3.put("child", "进程保护策略");
				childMap3.put("key", index + "");
				childMap3.put("activityName", "");
				varList.add(childMap3);

				Map<String, String> childMap4 = new HashMap<String, String>();
				childMap4.put("child", "进程访问控制");
				childMap4.put("key", index + "");
				childMap4.put("activityName", "");
				varList.add(childMap4);

				Map<String, String> childMap5 = new HashMap<String, String>();
				childMap5.put("child", "窗口控制");
				childMap5.put("key", index + "");
				childMap5.put("activityName", "");
				varList.add(childMap5);
			} else if ((index == 2) && (i == 2)) {
				Map<String, String> childMap0 = new HashMap<String, String>();
				childMap0.put("child", "用户密码策略");
				childMap0.put("key", index + "");
				childMap0.put("activityName", "");
				varList.add(childMap0);

				Map<String, String> childMap1 = new HashMap<String, String>();
				childMap1.put("child", "用户权限策略");
				childMap1.put("key", index + "");
				childMap1.put("activityName", "");
				varList.add(childMap1);

				Map<String, String> childMap2 = new HashMap<String, String>();
				childMap2.put("child", "协议防火墙策略");
				childMap2.put("key", index + "");
				childMap2.put("activityName", "");
				varList.add(childMap2);

				Map<String, String> childMap3 = new HashMap<String, String>();
				childMap3.put("child", "应用防火墙策");
				childMap3.put("key", index + "");
				childMap3.put("activityName", "");
				varList.add(childMap3);
			} else if ((index == 2) && (i == 3)) {
				Map<String, String> childMap0 = new HashMap<String, String>();
				childMap0.put("child", "防违规外联策略");
				childMap0.put("key", index + "");
				childMap0.put("activityName", "com.vrv.netmgr.strategy.PrevIllExtConnActivity");
				varList.add(childMap0);

				Map<String, String> childMap1 = new HashMap<String, String>();
				childMap1.put("child", "防违规外联扩展策略");
				childMap1.put("key", index + "");
				childMap1.put("activityName", "");
				varList.add(childMap1);

				Map<String, String> childMap2 = new HashMap<String, String>();
				childMap2.put("child", "代理上网管理");
				childMap2.put("key", index + "");
				childMap2.put("activityName", "");
				varList.add(childMap2);
			}
			childs.add(varList);
		}
		DataSetUtil.checkDataSource(this.groups, this.childs);
	}

	/**
	 *
	 */
	Handler backHandler = new Handler(){
		public void handleMessage(Message msg){
			isBack = false;
		}
	};
	@Override
	public void onBackPressed() {
		LoginInfo loginInfo = LoginSharedPreferencesHelper.getLoginInfo(getApplicationContext());
		if (!isTop) {
			initViewSetting();
		} else if (!isBack) {
			isBack = true;
			if(loginInfo.isAutoLogin()){
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getApplicationContext(), "再按一次到登录界面", Toast.LENGTH_SHORT).show();
			}
			backHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			if(loginInfo.isAutoLogin()){
				finish();
				System.exit(0);
			}else{
				finish();
				startActivity(new Intent(StrategyActivity.this, LoginActivity.class));
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isNavgate) {
			initViewSetting();
		} else {
			isNavgate = Boolean.TRUE;
		}
	}
}
